package il.ac.tau.cvws.scrabblesidekick.client.ui.view;

import il.ac.tau.cvws.scrabblesidekick.client.R;
import il.ac.tau.cvws.scrabblesidekick.client.app.controller.GameController;
import il.ac.tau.cvws.scrabblesidekick.client.util.dictionary.hebrew.HebrewFilter;
import il.ac.tau.cvws.scrabblesidekick.shared.constant.BoardConstants;
import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;

/* The view for the rack screen.
 */
@SuppressLint("ViewConstructor")
public class RackView extends LinearLayout
{
	private GameController _controller;
	private Context 	   _context;
	private EditText  	   _currentTile;
	
	private EditText _tile1;
	private EditText _tile2;
	private EditText _tile3;
	private EditText _tile4;
	private EditText _tile5;
	private EditText _tile6;
	private EditText _tile7;
	
	public RackView(GameController contoller, Context context)
	{
		super(context);
		
		_context    = context;
		_controller = contoller;
		
		initLayout(context);
	}

	private void initTiles()
	{
		_tile1 = (EditText)findViewById(R.id.tile1);
		_tile2 = (EditText)findViewById(R.id.tile2);
		_tile3 = (EditText)findViewById(R.id.tile3);
		_tile4 = (EditText)findViewById(R.id.tile4);
		_tile5 = (EditText)findViewById(R.id.tile5);
		_tile6 = (EditText)findViewById(R.id.tile6);
		_tile7 = (EditText)findViewById(R.id.tile7);
		
		_tile1.setFilters(new InputFilter[]{new HebrewFilter(), new InputFilter.LengthFilter(1)});
		_tile2.setFilters(new InputFilter[]{new HebrewFilter(), new InputFilter.LengthFilter(1)});
		_tile3.setFilters(new InputFilter[]{new HebrewFilter(), new InputFilter.LengthFilter(1)});
		_tile4.setFilters(new InputFilter[]{new HebrewFilter(), new InputFilter.LengthFilter(1)});
		_tile5.setFilters(new InputFilter[]{new HebrewFilter(), new InputFilter.LengthFilter(1)});
		_tile6.setFilters(new InputFilter[]{new HebrewFilter(), new InputFilter.LengthFilter(1)});
		_tile7.setFilters(new InputFilter[]{new HebrewFilter(), new InputFilter.LengthFilter(1)});
		
		_tile1.addTextChangedListener(new TileWatch());
		_tile2.addTextChangedListener(new TileWatch());
		_tile3.addTextChangedListener(new TileWatch());
		_tile4.addTextChangedListener(new TileWatch());
		_tile5.addTextChangedListener(new TileWatch());
		_tile6.addTextChangedListener(new TileWatch());
		_tile7.addTextChangedListener(new TileWatch());
		
		_currentTile = _tile1;		
	}
	
	private void initLayout(Context context)
	{		
		LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.activity_rack, this);

		initTiles();
	}
	
	public void showKeyboard()
	{
		toogleKeyboard();
	}
	
	private void toogleKeyboard()
	{
        InputMethodManager imm = (InputMethodManager)_context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, 0);
	}
	
	private class TileWatch implements TextWatcher
	{
		@Override
		public void afterTextChanged(Editable s)
		{
			findNextFocus();
			
			if (isRackReady())
			{
				submitRack();
			}		
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after)
		{
			//DO NOTHING
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count)
		{
			//DO NOTHING			
		}
	}
	
	/* Find next focus. Make sure all letters get entered, no skipping allowed.
	 */
	private void findNextFocus()
	{
		_currentTile.clearFocus();
		
		 if (_tile1.getText().length() == 0)
		 {
			 _tile1.requestFocus();
			 _currentTile = _tile1;
		 } 
		 else if (_tile2.getText().length() == 0)
		 {
			 _tile2.requestFocus();
			 _currentTile = _tile2;			 
		 }
		 else if (_tile3.getText().length() == 0)
		 {
			 _tile3.requestFocus();
			 _currentTile = _tile3;
		 }
		 else if (_tile4.getText().length() == 0)
		 {
			 _tile4.requestFocus();
			 _currentTile = _tile4;
		 }
		 else if (_tile5.getText().length() == 0)
		 {
			 _tile5.requestFocus();
			 _currentTile = _tile5;
		 }
		 else if (_tile6.getText().length() == 0)
		 {
			 _tile6.requestFocus();
			 _currentTile = _tile6;
		 }
		 else if (_tile7.getText().length() == 0)
		 {
			 _tile7.requestFocus();
			 _currentTile = _tile7;
		 }
	}
	
	/* Once the rack is full, submit it to the controller.
	 */
	private void submitRack()
	{
		final char[] tiles = new char[7];

		tiles[1] = (_tile1.getText().length() == 0 ? BoardConstants.EMPTY_TILE : _tile1.getText().charAt(0));
		tiles[2] = (_tile2.getText().length() == 0 ? BoardConstants.EMPTY_TILE : _tile2.getText().charAt(0));
		tiles[3] = (_tile3.getText().length() == 0 ? BoardConstants.EMPTY_TILE : _tile3.getText().charAt(0));
		tiles[4] = (_tile4.getText().length() == 0 ? BoardConstants.EMPTY_TILE : _tile4.getText().charAt(0));
		tiles[5] = (_tile5.getText().length() == 0 ? BoardConstants.EMPTY_TILE : _tile5.getText().charAt(0));
		tiles[6] = (_tile6.getText().length() == 0 ? BoardConstants.EMPTY_TILE : _tile6.getText().charAt(0));
		tiles[0] = (_tile7.getText().length() == 0 ? BoardConstants.EMPTY_TILE : _tile7.getText().charAt(0));
		
		toogleKeyboard();
		
		_controller.handleRack(tiles);	
	}
	
	/* Tests whether or not all tiles have been entered and the rack is ready to be submitted.
	 */
	private boolean isRackReady()
	{
		boolean flag = true;
		
		flag = flag && (_tile1.getText().length() > 0);
		flag = flag && (_tile2.getText().length() > 0);
		flag = flag && (_tile3.getText().length() > 0);
		flag = flag && (_tile4.getText().length() > 0);
		flag = flag && (_tile5.getText().length() > 0);
		flag = flag && (_tile6.getText().length() > 0);
		flag = flag && (_tile7.getText().length() > 0);
		
		return flag;
	}
}
